/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package network.szyfrowanie;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

/**
 *
 * @author mikolaj
 */
public class SzyfrowanieRSA {
    
     public byte[] szyfruj(byte[] b,PublicKey k) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
         return szyfruj(b,k,0);
     }
     
     public byte[] szyfruj(byte[] b,PublicKey k,int t) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
        
         int d=117;
         if(t==1)
             d=128;
         if(b.length>d){
            byte[] tmp1=new byte[d];
            System.arraycopy(b, 0, tmp1, 0, d);
            byte[] tmp2=new byte[b.length-d];
            byte[] ret1=szyfruj(tmp1,k,t);
            System.arraycopy(b, d, tmp2, 0, b.length-d);
            byte[] ret2=szyfruj(tmp2,k,t);
            byte[] ret=new byte[ret1.length+ret2.length];
            System.arraycopy(ret1, 0, ret, 0, ret1.length);
            System.arraycopy(ret2, 0, ret,ret1.length, ret2.length);
            return ret;
            
        }
	Cipher cipher = Cipher.getInstance(k.getAlgorithm());
        if(t==0)
            cipher.init(Cipher.ENCRYPT_MODE, k);
        if(t==1)
            cipher.init(Cipher.DECRYPT_MODE, k);
        byte[] encrypted = cipher.doFinal(b);
        return encrypted;
    }
     public byte[] odszyfruj(byte[] b,PrivateKey k) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
         return odszyfruj(b,k,0);
     }
        public byte[] odszyfruj(byte[] b,PrivateKey k,int t) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
       
            int d=128;
            if(t==1)
                d=117;
            if(b.length>d){
            byte[] tmp1=new byte[d];
            System.arraycopy(b, 0, tmp1, 0, d);
            byte[] tmp2=new byte[b.length-d];
            byte[] ret1=odszyfruj(tmp1,k,t);
            System.arraycopy(b, d, tmp2, 0, b.length-d);
            byte[] ret2=odszyfruj(tmp2,k,t);
            byte[] ret=new byte[ret1.length+ret2.length];
            System.arraycopy(ret1, 0, ret, 0, ret1.length);
            System.arraycopy(ret2, 0, ret,ret1.length, ret2.length);
            return ret;
            
        }
	Cipher cipher = Cipher.getInstance(k.getAlgorithm());
        if(t==0)
            cipher.init(Cipher.DECRYPT_MODE, k);
        if(t==1)
            cipher.init(Cipher.ENCRYPT_MODE, k);
	byte[] decrypted = cipher.doFinal(b);
        return decrypted;
    }
}
